package com.hsogoo.collate.batch;

import com.hsogoo.collate.domain.CollateFieldsDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.BeanUtils;

/**
 * 把文件读取到的数据转成数据库需保存的字段
 * @Author huangsaigang
 * Created @2019-10-30.
 */
@Slf4j
public class CollateProcessor implements ItemProcessor<CollateFields, CollateFieldsDO> {

    /**
     * processor通过StepExecution获取
     * jobParameters
     * jobExecutionContext
     */

    /**JobParameters在运行时没法修改，只能在启动时设置属性，启动后只能获取get属性*/
    private String taskName;
    private String targetCollateDate;
    /**jobExecutionContext可以在运行时设置属性，put新的属性*/
    private Long recordId;

    public CollateProcessor(){}

    public CollateProcessor(String taskName, String targetCollateDate, Long recordId) {
        this.taskName = taskName;
        this.targetCollateDate = targetCollateDate;
        this.recordId = recordId;
    }

    @Override
    public CollateFieldsDO process(CollateFields collateFields) throws Exception {
        CollateFieldsDO collateFieldsDO = new CollateFieldsDO();
        BeanUtils.copyProperties(collateFields, collateFieldsDO);
        collateFieldsDO.setRecordId(recordId);
        collateFieldsDO.setTaskName(taskName);
        collateFieldsDO.setCollateDate(targetCollateDate);
        collateFieldsDO.initTime();
        return collateFieldsDO;
    }

}
